package com.wangzhenyu.learn.test.webview

import android.content.Context
import android.os.Bundle
import android.webkit.JavascriptInterface
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import com.wangzhenyu.common.util.log.LogUtil
import com.wangzhenyu.learn.R

class WebViewOtherActivity : AppCompatActivity() {

    private lateinit var webView: WebView
    private val url = "https://news.zhibo8.com/zuqiu/2023-06-10/64826ba4a5f31native.htm"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_web_view_other)

        webView = findViewById(R.id.webView)

        val setting = webView.settings

        //设置支持JS代码
        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        //若加载的 html 里有JS 在执行动画等操作，会造成资源浪费（CPU、电量）
        //在 onStop 和 onResume 里分别把 setJavaScriptEnabled() 给设置成 false 和 true 即可
        setting.javaScriptEnabled = true
        setting.allowFileAccess = true //设置支持打开文件
        setting.allowFileAccessFromFileURLs = true
        webView.loadUrl(url)


        webView.webViewClient = object : WebViewClient() {

            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                view.loadUrl(url)
                return false
            }

            //监听网页是否加载完成
            override fun onPageFinished(view: WebView, url: String) {
                super.onPageFinished(view, url)
                //监听webView已经将页面加载完成，此时添加对图片的监听
                addImageClickListener(webView)
            }
        }

        //设置处理js代码的客户端
        webView.webChromeClient = WebChromeClient()

        //因为在JS中调用了Android代码，需要设置通道
        webView.addJavascriptInterface(NetJavaScript(this), "listen")
    }

    private fun addImageClickListener(view: WebView) {
        webView.loadUrl("javascript:( function(){\n" +
                "            var objs = document.getElementsByTagName(\"img\");\n" +
                "            var arr=[];\n" +
                "            for(var i=0;i<objs.length;i++){\n" +
                "                arr[i]=objs[i].getAttribute('src');\n" +
                "            }\n" +
                "            for(var i=0;i<objs.length;i++){\n" +
                "                objs[i].onclick=function(){\n" +
                "                window.listen.openImage(arr,this.getAttribute('src'));\n" +
                "                }\n" +
                "            }\n" +
                "\n" +
                "        })")
    }
}

class NetJavaScript(private val context: Context) {


    @JavascriptInterface
    fun openImage(images: Array<String>, image: String) {
        LogUtil.d("WZYTest","点击了")
        val size: Int = images.size
        var position = 0
        for (i in 0 until size) {
            if (image == images[i]) {
                position = i
            }
        }
        LogUtil.d("WZYTest", "position: $position")
        LogUtil.d("WZYTest", "image: $image")
    }

}